import os
import time

import pytest

from common.readData import readData
from common.Assert import Assert
from common.DButils import select,back_up,rollback
from common import loginOperater
from selenium import webdriver
from common.comDB import comDB
from common.serviceWeb import ServiceWeb
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

com_db=comDB()
read_data=readData()
class TestBankcard:
    @pytest.mark.parametrize("username,password,cardBank,type,cardNum,expect", read_data.read_data(3, 6))
    def test_add_bankcard(self, username, password, cardBank, type, cardNum, expect, ):

        service = ServiceWeb()

        backup = []

        try:
            #拉起浏览器
            chromeDriver2 = service.get_driver("http://localhost:90/")
            # 链接数据库
            con, cursor = com_db.get_con()
            # 数据库备份
            backup = back_up("bankcard")

            # 登录
            login = loginOperater.LoginOperater()
            login.login(chromeDriver2, username, password)
            time.sleep(1)

            # 进入银行卡管理
            chromeDriver2.get("http://localhost:90/user/personal/toBankCard.html")
            # 初始化显式等待对象
            wait = WebDriverWait(chromeDriver2, 10)

            # 新增银行卡
            el6 = wait.until(
                EC.element_to_be_clickable((By.ID, "bankCard_add_modal_btn"))
            )
            el6.click()

            # 等待银行输入框加载完成
            el7 = wait.until(
                EC.visibility_of_element_located((By.ID, "cardBank_add_input"))
            )
            el7.send_keys(cardBank)

            if type == 1:
                xpath="//*[@id='bankCardAddModal']/div/div/div[2]/form/div[2]/div[1]/div/label[1]/span"
            else:
                xpath="//*[@id='bankCardAddModal']/div/div/div[2]/form/div[2]/div[1]/div/label[2]/span"

            el8 = wait.until(
                EC.element_to_be_clickable((By.XPATH, xpath))
            )
            el8.click()

            el9 = chromeDriver2.find_element(By.ID, "cardNum_add_input")
            el9.send_keys(cardNum)

            el10 = wait.until(
                EC.element_to_be_clickable((By.ID, "bankCard_save_btn"))
            )
            el10.click()

            # 查询银行卡包
            sql1 = "SELECT * FROM bankcard WHERE  cardNum=%s"
            """cursor.execute(sql1, (cardNum), )
            con.commit()"""
            result = len(select(sql1,(cardNum,)))

            #断言
            screenshot_dir = "D:\pythonproject\自动化_1封装\失败截图\addbankcard"
            screenshot_path = f"{screenshot_dir}/{username}失败截图.png"
            Assert(chromeDriver2,result,expect,screenshot_path)

        finally:
            # 数据库恢复
            rollback(backup, "bankcard")
            #资源清理
            cursor.close()
            con.close()
            chromeDriver2.quit()

    @pytest.mark.parametrize("username,password,yes,expect", read_data.read_data(4, 4))
    def test_delete_bankcard(self, username, password, yes, expect):

        service = ServiceWeb()

        backup = []

        try:
            # 拉起浏览器
            chromeDriver2 = service.get_driver("http://localhost:90/")
            # 链接数据库
            con, cursor = com_db.get_con()
            # 数据库备份
            backup = back_up("bankcard")

            # 登录
            login = loginOperater.LoginOperater()
            login.login(chromeDriver2, username, password)
            time.sleep(1)

            # 进入银行卡管理
            chromeDriver2.get("http://localhost:90/user/personal/toBankCard.html")
            # 初始化显式等待对象
            wait = WebDriverWait(chromeDriver2, 10)

            # 删除银行卡
            el6 = wait.until(
                EC.element_to_be_clickable((By.XPATH,
                                            "/html/body/div/div/main/div/div/div[2]/div/div/div[1]/ul/li/button[2]"))
            )
            el6.click()

            sql1 = "SELECT * FROM bankcard"
            result1 = len(select(sql1,()))

            if yes == 1:
                xpath="/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[1]"
            else:
                xpath="/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[2]"

            el7= wait.until(
                EC.element_to_be_clickable((By.XPATH, xpath))
            )
            el7.click()

            # 查询银行卡包
            result2 = len(select(sql1,()))
            result=result1-result2

            #断言
            screenshot_dir = "D:\pythonproject\自动化_1封装\失败截图\deletebankcard"
            screenshot_path = f"{screenshot_dir}/{username}失败截图.png"
            Assert(chromeDriver2,result,expect,screenshot_path)

        finally:
            # 数据库恢复
            rollback(backup, "bankcard")
            # 资源清理
            cursor.close()
            con.close()
            chromeDriver2.quit()

    @pytest.mark.parametrize("username,password,newname,type,newNum,expect", read_data.read_data(5, 6))
    def test_edit_bankcard(self, username, password, newname, type, newNum, expect):
        service = ServiceWeb()

        backup = []

        try:
            # 拉起浏览器
            chromeDriver2 = service.get_driver("http://localhost:90/")
            # 链接数据库
            con, cursor = com_db.get_con()
            # 数据库备份
            backup = back_up("bankcard")

            # 登录
            login = loginOperater.LoginOperater()
            login.login(chromeDriver2, username, password)
            time.sleep(1)

            # 进入银行卡管理
            chromeDriver2.get("http://localhost:90/user/personal/toBankCard.html")
            # 初始化显式等待对象
            wait = WebDriverWait(chromeDriver2, 10, 0.5)

            # 编辑银行卡
            el4 = wait.until(
            EC.element_to_be_clickable((By.XPATH,
                                            "/html/body/div/div/main/div/div/div[2]/div/div/div[1]/ul/li/button[1]"))
            )
            el4.click()

            # 编辑银行卡名称
            el5 = wait.until(
                EC.visibility_of_element_located((By.ID, "cardBank_update_input"))
            )
            el5.clear()
            el5.send_keys(newname)

            # 编辑银行卡类型
            if type == 1:
                el6 = chromeDriver2.find_element(By.XPATH,
                                                "/html/body/div[1]/div/main/div/div/div[6]/div/div/div[2]/form/div[2]/div[1]/div/label[1]/span")
            elif type == 2:
                el6 = chromeDriver2.find_element(By.XPATH,
                                                "/html/body/div[1]/div/main/div/div/div[6]/div/div/div[2]/form/div[2]/div[1]/div/label[2]/span")

            el6.click()
            time.sleep(1)

            # 编辑银行卡号
            el8 = wait.until(
                EC.visibility_of_element_located((By.ID, "cardNum_update_input"))
            )
            el8.clear()
            el8.send_keys(newNum)

            el9 = wait.until(
                EC.element_to_be_clickable((By.ID, "bankCard_update_btn"))
            )
            el9.click()
            time.sleep(1)

            # 查询数据库
            sql1 = ("SELECT cardBank,type,cardNum FROM bankcard "
                    "where cardBank=%s and type=%s and cardNum=%s")
            """cursor.execute(sql1, (newname, type, newNum))
            con.commit()"""
            result = len(select(sql1, (newname, type, newNum)))

            # 断言
            screenshot_dir = "D:\pythonproject\自动化_1封装\失败截图\editbankcard"
            screenshot_path = f"{screenshot_dir}/{username}失败截图.png"
            Assert(chromeDriver2, result, expect, screenshot_path)

        finally:
            # 数据库恢复
            rollback(backup, "bankcard")
            # 资源清理
            cursor.close()
            con.close()
            chromeDriver2.quit()